<template>
  <div class="player-management">
    <h2>参赛选手管理</h2>
    <div class="action-buttons">
      <el-button type="primary" @click="showAddDialog">添加选手</el-button>
    </div>
    <el-table :data="players" style="width: 100%">
      <el-table-column prop="id" label="选手ID" width="180" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="school_name" label="学校" />
      <el-table-column prop="grade_level_name" label="年级组别" />
      <el-table-column prop="gender" label="性别" />
      <el-table-column prop="birth_date" label="出生年月" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="editPlayer(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deletePlayer(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加选手' : '编辑选手'"
      width="30%"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="学校">
          <el-select v-model="form.school_id" placeholder="请选择学校">
            <el-option
              v-for="school in schools"
              :key="school.id"
              :label="school.name"
              :value="school.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年级组别">
          <el-select v-model="form.grade_level_id" placeholder="请选择年级组别">
            <el-option
              v-for="level in gradeLevels"
              :key="level.id"
              :label="level.name"
              :value="level.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生年月">
          <el-date-picker
            v-model="form.birth_date"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="照片">
          <el-input v-model="form.photo_url" placeholder="照片URL" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const players = ref([])
const schools = ref([])
const gradeLevels = ref([])
const dialogVisible = ref(false)
const dialogType = ref('add')
const form = ref({
  id: null,
  name: '',
  school_id: '',
  grade_level_id: '',
  gender: '',
  birth_date: '',
  photo_url: ''
})

// 获取选手列表
const fetchPlayers = async () => {
  try {
    const response = await axios.get('http://localhost:3000/api/players')
    players.value = response.data
  } catch (error) {
    ElMessage.error('获取选手列表失败')
  }
}

// 获取学校列表
const fetchSchools = async () => {
  try {
    const response = await axios.get('http://localhost:3000/api/schools')
    schools.value = response.data
  } catch (error) {
    ElMessage.error('获取学校列表失败')
  }
}

// 获取年级组别列表
const fetchGradeLevels = async () => {
  try {
    const response = await axios.get('http://localhost:3000/api/grade-levels')
    gradeLevels.value = response.data
  } catch (error) {
    ElMessage.error('获取年级组别列表失败')
  }
}

// 显示添加对话框
const showAddDialog = () => {
  dialogType.value = 'add'
  form.value = {
    name: '',
    school_id: '',
    grade_level_id: '',
    gender: '',
    birth_date: '',
    photo_url: ''
  }
  dialogVisible.value = true
}

// 编辑选手
const editPlayer = (player) => {
  dialogType.value = 'edit'
  form.value = { ...player }
  dialogVisible.value = true
}

// 删除选手
const deletePlayer = async (id) => {
  try {
    await ElMessageBox.confirm('确定要删除这个选手吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await axios.delete(`http://localhost:3000/api/players/${id}`)
    ElMessage.success('删除成功')
    fetchPlayers()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (dialogType.value === 'add') {
      await axios.post('http://localhost:3000/api/players', form.value)
      ElMessage.success('添加成功')
    } else {
      await axios.put(`http://localhost:3000/api/players/${form.value.id}`, form.value)
      ElMessage.success('更新成功')
    }
    dialogVisible.value = false
    fetchPlayers()
  } catch (error) {
    ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
  }
}

onMounted(() => {
  fetchPlayers()
  fetchSchools()
  fetchGradeLevels()
})
</script>

<style scoped>
.player-management {
  padding: 20px;
}

.action-buttons {
  margin-bottom: 20px;
}
</style>